package com.atguigu.webadmin.service.impl;

import com.atguigu.model.pojo.SystemPost;
import com.atguigu.model.pojo.SystemUser;
import com.atguigu.webadmin.mapper.SystemUserMapper;
import com.atguigu.webadmin.service.SystemPostService;
import com.atguigu.webadmin.service.SystemUserService;
import com.atguigu.webadmin.vo.system.user.SystemUserItemVo;
import com.atguigu.webadmin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @Author 梅超凡
 * @Date 2024/11/12~20:49
 * @Description 针对表【system_user(员工信息表)】的数据库操作Service实现
*/
@Service
@Slf4j
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    /**
     * @Author 梅超凡
     * @Date 2024/11/16~15:20
     * @Description 多表查询用户信息
    */
    @Override
    public IPage<SystemUserItemVo> pageSystemUserByQuery(IPage<SystemUserItemVo> iPage,
                                                         SystemUserQueryVo queryVo) {
        IPage<SystemUserItemVo> voIPage = systemUserMapper.pageSystemUserByQuery(iPage,queryVo);
        log.info("查询结果为：{}",voIPage);
        return voIPage;
    }

    @Autowired
    private SystemPostService systemPostService;
    
    /**
     * @Author 梅超凡 
     * @Date 2024/11/16~16:41
     * @Description 根据id查询用户信息
    */
    @Override
    public SystemUserItemVo getSystemUserById(Long id) {
        SystemUser systemUser = getById(id);
        //将密码置空
        systemUser.setPassword(null);
        //密码是会查询到的!  select = false | mapper service password = null | 自定义方法 select 不写password
        SystemPost systemPost = systemPostService.getById(systemUser.getPostId());
        //创建vo对象
        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
        //属性对拷
        BeanUtils.copyProperties(systemUser,systemUserItemVo);
        systemUserItemVo.setPostName(systemPost.getName());
        return systemUserItemVo;
    }

    /**
     * @Author 梅超凡
     * @Date 2024/11/16~20:04
     * @Description 更新用户信息，需要把传入的密码进行加密后再插入数据库中
    */
    @Override
    public Boolean saveOrUpdateSystemUser(SystemUser systemUser) {
        systemUser.setPassword(DigestUtils.sha256Hex(systemUser.getPassword()));
        return super.saveOrUpdate(systemUser);
    }
}
